library TOTMission initializer init requires LegacySystem



	private  function Trig_TOT_Conditions takes nothing returns boolean
		if ( not ( GetSpellAbilityId() == 'A02U' ) ) then
			return false
		endif
		return true
	endfunction

	// drops a bomb and damages area
	private  function dropBombTOT takes unit source, location l,integer damage returns nothing
	   
		local integer explosion_damage_radius = 300   
		local rect r = Rect(GetLocationX(l)-explosion_damage_radius,GetLocationY(l)-explosion_damage_radius,GetLocationX(l)+explosion_damage_radius,GetLocationY(l)+explosion_damage_radius)

		// create effects
		call AddSpecialEffectLocBJ( l, "Effect_Artillery.mdx" )
		call DestroyEffectBJ( GetLastCreatedEffectBJ() )
		call AddSpecialEffectLocBJ( l, "Objects\\Spawnmodels\\Other\\NeutralBuildingExplosion\\NeutralBuildingExplosion.mdl" )
		call DestroyEffectBJ( GetLastCreatedEffectBJ() )

		// damage area
		call DamageTrees(r,damage)
		call DamageAreaExplosionBJ(source,l,damage,explosion_damage_radius)
		call RemoveRect(r)
		set r = null
	endfunction

	private  function dropBombsTOT takes unit source, location target, integer howmany returns nothing

		 local location l
		 local integer damage = 1000 
		 local integer i = 0
		 local real barrage_delay = 0.3
		 local real d
		 local real d2
		 
		 local real safe_aoe = 1400
		 local real max_aoe = 3000
		 loop
			 exitwhen i > howmany
			 set d = GetRandomDirectionDeg()
			 set d2 = GetRandomReal(1,70)
			 set l = PolarProjectionBJ(target, GetRandomReal(safe_aoe, max_aoe), d)
			 call dropBombTOT(source,l,damage)
			 call RemoveLocation(l)
			 set l = PolarProjectionBJ(target, GetRandomReal(safe_aoe, max_aoe), d+d2-90)
			 call dropBombTOT(source,l,damage)
			 call RemoveLocation(l)
			 set i = i + 2
			 set l = PolarProjectionBJ(target, GetRandomReal(safe_aoe, max_aoe), d+d2)
			 call dropBombTOT(source,l,damage)
			 call RemoveLocation(l)
			 set l = PolarProjectionBJ(target, GetRandomReal(safe_aoe, max_aoe), d+d2+90)
			 call dropBombTOT(source,l,damage)
			 call RemoveLocation(l)
			 set i = i + 2
			 call TriggerSleepAction(barrage_delay)
		 endloop
		 set l = null

	endfunction


	//===========================================================================
	// Entry point for the spell
	//===========================================================================

	private  function Trig_TOT_Actions takes nothing returns nothing
		
		// variables
		local integer spell_level= GetUnitAbilityLevelSwapped(GetSpellAbilityId(), GetTriggerUnit())
		local location target = GetSpellTargetLoc()

		local integer total_shells = getInteger(null,"FO_total_shells") 

		local integer number_of_shells = 60
		local integer barrage_delay = 10

	  
		local unit caster = GetTriggerUnit()

		// core
		// check for shells and substract number per barrage
		if(number_of_shells > total_shells) then 
			 call print ("Shells Depleted")  
			 call RemoveLocation(target)
			 set target = null
			 set caster = null
			 return
		endif 
		call putInteger(null,"FO_total_shells", total_shells - number_of_shells)
		call print("shells remaining: " + I2S(total_shells - number_of_shells)) 

		call TriggerSleepAction(barrage_delay)

		// drop the shells, Note: this function has wait calls of 1-3 seconds total 
		call dropBombsTOT(caster,target, number_of_shells*3) // in azazel's code its 175!!!
	  
		// cleanup
		call RemoveLocation(target)
		set target = null
	  
		set caster = null
	endfunction



	//===========================================================================
	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger(  )
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
		call TriggerAddCondition( t, Condition( function Trig_TOT_Conditions ) )
		call TriggerAddAction( t, function Trig_TOT_Actions )
	endfunction




endlibrary